package com.xianggu.designMode.chainMethod.demo03;

import com.xianggu.designMode.chainMethod.demo03.impl.CompositeValidator;
import com.xianggu.designMode.chainMethod.demo03.impl.InventoryHandler;
import com.xianggu.designMode.chainMethod.demo03.impl.PaymentHandler;
import com.xianggu.designMode.chainMethod.demo03.impl.ShippingHandler;

public class OrderProcessingClient {
    public static void main(String[] args) {
        // 构建订单
        Order order = new Order();
        order.setOrderId("20231015001");
        order.setItemCount(5);
        order.setTotalAmount(999.99);
        order.setPaid(true);
        order.setShippingAddress("北京市海淀区");

        // 构建处理链
        OrderHandler chain = new InventoryHandler()
                .setNext(new PaymentHandler())
                .setNext(new ShippingHandler());

        // 执行处理
        OrderResult result = chain.handle(order);
        if (result.isSuccess()) {
            System.out.println("订单处理成功!");
        } else {
            System.out.printf("订单处理失败：%s（失败环节：%s）%n", 
                result.getMessage(), result.getFailedStep());
        }

        // 1. 组合模式（Composite）
        //场景：需要批量处理多个校验规则
        //实现：创建组合校验器，内部管理多个子校验器
        CompositeValidator addressCheck = new CompositeValidator()
                .add(new InventoryHandler())
                .add(new PaymentHandler())
                .add(new ShippingHandler());
        OrderResult result2 = addressCheck.handle(order);

        if (result2.isSuccess()) {
            System.out.println("订单处理成功!");
        } else {
            System.out.printf("订单处理失败：%s（失败环节：%s）%n",
                    result.getMessage(), result.getFailedStep());
        }


    }
}